import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:o2oa_all_platform/common/index.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import 'index.dart';

class CmsEditDocumentPage extends GetView<CmsEditDocumentController> {
  const CmsEditDocumentPage({Key? key}) : super(key: key);

  static void open() {
    Get.toNamed(O2OARoutes.cmsDocumentEdit);
  }

  // 主视图
  Widget _buildView() {
    return Obx(() => SmartRefresher(
        enablePullDown: true,
        enablePullUp: controller.state.hasMoreData,
        controller: controller.refreshController,
        onRefresh: () => controller.onRefresh(),
        onLoading: () => controller.loadMoreData(),
        child: ListView.separated(
            separatorBuilder: (context, index) => const Divider(height: 1),
            itemCount: controller.state.docList.length,
            itemBuilder: (context, index) {
              final item = controller.state.docList[index];
              final name = '[${item.categoryName ?? ''}]${item.title ?? ''}';
              return ListTile(
                onTap: () => controller.clickEditDoc(item),
                title: Text(name, style: Theme.of(context).textTheme.bodyLarge),
                subtitle: Text(item.createTime?.substring(0, 10) ?? '',
                    style: Theme.of(context).textTheme.bodyMedium),
                trailing: Row(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    Text(
                      docStatusText(item.docStatus ?? ''),
                      style: Theme.of(context).textTheme.bodySmall,
                    ),
                    O2UI.rightArrow()
                  ],
                ),
              );
            })));
  }

  String docStatusText(String docStatus) {
    switch (docStatus) {
      case 'published':
        return 'cms_document_status_published'.tr;
      case 'draft':
        return 'cms_document_status_draft'.tr;
      case 'waitPublish':
        return 'cms_document_status_waitPublish'.tr;
      default:
        return '';
    }
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<CmsEditDocumentController>(
      builder: (_) {
        return Scaffold(
          appBar: AppBar(
            title: Text("cms_edit_document_title".tr),
            actions: [
              TextButton(
                  onPressed: controller.clickPublish,
                  child: Text('cms_document_publish'.tr,
                      style: AppTheme.whitePrimaryTextStyle))
            ],
          ),
          body: SafeArea(
            child: Obx(() => controller.state.docList.length > 0
                ? _buildView()
                : O2UI.noResultView(context)),
          ),
        );
      },
    );
  }
}
